package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import graphic.Edge;
import graphic.Graph;

/**
 * @author ZhangyiA
 * @describe: 从文件中读取图数据
 * @date 2021/06/22
 */

public class GraphReader {

    public GraphReader(){}

    public Graph load(String path) throws IOException {
        File file = new File(path);
        assert file.exists() : String.format("File path %s isn't exists , please check it." , file);
        Map<Integer , ArrayList<Edge>> label_edges = new HashMap<>(); // 根据边标签 , 将边聚类
        Map<Integer , ArrayList<Integer>> label_vertex = new HashMap<>(); // 根据顶点标签 , 将顶点聚类
        Graph graph = new Graph();
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line;
        while((line = reader.readLine()) != null){
            line = line.trim();
            if(line.equals("")){
                continue;
            }
            String[] buff = line.split("\\s+");
            if(buff[0].equals("c")){
                continue;
            } else if(buff[0].equals("v")){
                int val = Integer.parseInt(buff[1]);
                int label = Integer.parseInt(buff[2]);
                ArrayList<Integer> temp = label_vertex.containsKey(label) ? label_vertex.get(label) : new ArrayList<>();
                temp.add(val);
                label_vertex.put(label , temp);
            } else if(buff[0].equals("e")){
                int vertexFrom = Integer.parseInt(buff[1]);
                int vertexTo = Integer.parseInt(buff[2]);
                int label = Integer.parseInt(buff[3]);
                Edge edge = new Edge(vertexFrom , vertexTo , label , 0);
                Edge edge_rev = new Edge(vertexTo , vertexFrom , label , 0); // 反相边
                ArrayList<Edge> temp = label_edges.containsKey(label) ? label_edges.get(label) : new ArrayList<>();
                temp.add(edge);
                temp.add(edge_rev);
                label_edges.put(label , temp);
            }
        }
        graph.setLabel_Vertex(label_vertex);
        graph.setLabel_Edges(label_edges);
//        getVertexFreqMax_Min(label_vertex);
//        getEdgeFreqMax_Min(label_edges);
        return graph;
    }

    public void getVertexFreqMax_Min(Map<Integer , ArrayList<Integer>> label_vertex){
        int max_freq = Integer.MIN_VALUE , min_freq = Integer.MAX_VALUE;
        for(Map.Entry<Integer , ArrayList<Integer>> entry : label_vertex.entrySet()){
            System.out.println(entry.getKey() + "  " + entry.getValue().size());
            max_freq = Math.max(max_freq , entry.getValue().size());
            min_freq = Math.min(min_freq , entry.getValue().size());
        }
        System.out.println("Vertex Freq :  max_freq = " + max_freq + "  min_freq = " + min_freq);
        System.out.println("min_sup = " + (max_freq + min_freq) / 2);
    }

    public void getEdgeFreqMax_Min(Map<Integer , ArrayList<Edge>> label_edges){
        int max_freq = Integer.MIN_VALUE , min_freq = Integer.MAX_VALUE;
        for(Map.Entry<Integer , ArrayList<Edge>> entry : label_edges.entrySet()){
            max_freq = Math.max(max_freq , entry.getValue().size());
            min_freq = Math.min(min_freq , entry.getValue().size());
        }
        System.out.println("Edge Freq :  max_freq = " + max_freq + "  min_freq = " + min_freq);
        System.out.println("min_sup = " + (max_freq + min_freq) / 2);
        int min_sup = 668;
        for(Map.Entry<Integer , ArrayList<Edge>> entry : label_edges.entrySet()){
            if(entry.getValue().size() >= min_sup){
                System.out.println(entry.getKey() + "  " + entry.getValue().size());
            }
        }
    }

}
